
exports.GAStoreIcon = UI.View.subclass ({
	classname: 'GAStoreIcon', 
	
	setup: function(storeObject) {
		this._storeObject = storeObject;
		if (this._storeObject != null) {
			this._resource = GAResourceUtils.getResourceOfType(storeObject, GAResourceType.kThumb);
			if (typeof this._resource != 'undefined' && this._resource != null) {
				this._isLoading = false;
				this._iconDownloader = null;
				this._iconImage = null;
				this._spinner = null;
				return true;
			}
			else {
				console.log("GAStoreIcon: Store object does not have a resource of type: Thumb");
				return false;
			}
		}
		else {
			console.log("GAStoreIcon: Store object found null, not loading any icon");
			return false;
		}
	},
	
	load: function(storeObject) {
		this.invalidate();
		var setupSuccessful = this.setup(storeObject);
		if (setupSuccessful == true) {
			if (this._isLoading == true) {
				return;
			}
			else {
				this.resetView();
				this._spinner = new UI.Spinner();
				var frame = this.getFrame();
				this._spinner.setFrame([(frame[2] - 32) / 2, (frame[3] - 32) / 2, 32, 32]);
				this.addChild(this._spinner);
				this._isLoading = true;
				this._iconDownloader = new GAResourceDownloader(this._storeObject, this.resourceDownloadingSuccessful.bind(this),
						this.resourceDownloadingFailed.bind(this), GAResourceType.kThumb);
				this._iconDownloader.startDownloading();
			}
		}
	},

	resourceDownloadingSuccessful: function(savePath) {
		this._isLoading = false;
		this._iconImage = new UI.Image();
		var filePath = savePath + "thumb." + this._resource.filetype;
		this._iconImage.setImage(filePath);
		var frame = this.getFrame();
		this._iconImage.setFrame([0, 0, frame[2], frame[3]]);
		this._iconImage.setImageFit(UI.ViewGeometry.FitModes.Stretch);
		this.addChild(this._iconImage);
		this.removeSpinner();
	},
	
	resourceDownloadingFailed: function(errorCode, errorMsg) {
		this._iconImage = null;
		this.removeSpinner();
	},
	
	resetView: function() {
		if (this._iconDownloader != null) {
			this._iconDownloader.destroy();
			this._iconDownloader = null;
		}
		if (this._iconImage != null) {
			this._iconImage.destroy();
			this._iconImage = null;
		}
		this._isLoading = false;
	},
	
	removeSpinner: function() {
		if (this._spinner != null){
			this._spinner.destroy();
			this._spinner = null;	
		}
	},
	
	invalidate: function() {
		this.removeSpinner();
		this.resetView();
		this._storeObject = null;
		this._resource = null;
	},
	
	destroy: function() {
		this.invalidate();
	}

});